import { createVNode, render } from 'vue'
import MessageConstructor from './message.vue';

let container = null;
const message = function (text, type) {
    if (container) {
        render(null, container);
        container = null;
    }

    container = document.createElement('div')

    const vnode = createVNode(
        MessageConstructor,
        {
            text: text,
            type: type,
            onDestroy: () => {
                render(null, container)
            },
        }
    )

    render(vnode, container)
    // instances will remove this item when close function gets called. So we do not need to worry about it.
    document.body.appendChild(container.firstElementChild)
};

export default message;